<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $color_header, $color_line1, $color_line2;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

BigTitle($txt['bnt_mines_title']);

//-------------------------------------------------------------------------------------------------

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Pull sector info into $sectorinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_universe' => $dbtables['universe'],
		'sector_id' => $playerinfo['sector']
	)
);
$sectorinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Get query string variables
if (isset($_POST['op']))
	$op = $_POST['op'];
elseif (isset($_GET['op']))
	$op = $_GET['op'];
else
	$op = '';

if (isset($_POST['nummines']))
	$nummines = $_POST['nummines'];

if (isset($_POST['numfighters']))
	$numfighters = $_POST['numfighters'];

if (isset($_POST['mode']))
	$mode = $_POST['mode'];

//Put the defence information into the array "defenceinfo"
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_sector_defence}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_sector_defence' => $dbtables['sector_defence'],
		'sector_id' => $playerinfo['sector']
	)
);

$i = 0;
$total_sector_fighters = 0;
$total_sector_mines = 0;
$owns_all = true;
$fighter_id = 0;
$mine_id = 0;
$set_attack = 'CHECKED';
$set_toll = '';

if ($smcFunc['db_num_rows']($result) > 0)
{
	while ($defences[$i] = $smcFunc['db_fetch_assoc']($result))
	{
		if ($defences[$i]['defence_type'] == 'F')
			$total_sector_fighters += $defences[$i]['quantity'];
		else
			$total_sector_mines += $defences[$i]['quantity'];

		if ($defences[$i]['ship_id'] != $playerinfo['ship_id'])
			$owns_all = false;
		else
		{
			if($defences[$i]['defence_type'] == 'F')
			{
				$fighter_id = $defences[$i]['defence_id'];
				if($defences[$i]['fm_setting'] == 'attack')
				{
					$set_attack = 'CHECKED';
					$set_toll = '';
				}
				else
				{
					$set_attack = '';
					$set_toll = 'CHECKED';
				}

			}
			else
				$mine_id = $defences[$i]['defence_id'];
		}
		$i++;
	}
}
$smcFunc['db_free_result']($result);

$num_defences = $i;

if ($playerinfo['turns'] < 1)
{
	echo $txt['bnt_mines_noturn'], '<br><br>';
	TextMainMenu();
	return;
}

$result = $smcFunc['db_query']('', '
	SELECT allow_defenses, {raw:db_universe}.zone_id, owner 
	FROM {raw:db_zones}, {raw:db_universe}
	WHERE sector_id = {int:sector_id}
	AND {raw:db_zones}.zone_id = {raw:db_universe}.zone_id',
	array(
		'db_universe' => $dbtables['universe'],
		'db_zones' => $dbtables['zones'],
		'sector_id' => $playerinfo['sector']
	)
);

$zoneinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($zoneinfo['allow_defenses'] == 'N')
	echo $txt['bnt_mines_nopermit'], '<br><br>';
else
{
	if ($num_defences > 0)
	{
		if (!$owns_all)
		{
			$defence_owner = $defences[0]['ship_id'];
			
			$result = $smcFunc['db_query']('', '
				SELECT *
				FROM {raw:db_ships}
				WHERE ship_id = {int:defence_owner}',
				array(
					'db_ships' => $dbtables['ships'],
					'defence_owner' => $defence_owner
				)
			);
			$fighters_owner = $smcFunc['db_fetch_assoc']($result);
			$smcFunc['db_free_result']($result);

			if ($fighters_owner['team'] != $playerinfo['team'] || $playerinfo['team'] == 0)
			{
				echo $txt['bnt_mines_nodeploy'], '<br>';
				TextMainMenu();
				return;
			}
		}
	}
	
	if ($zoneinfo['allow_defenses'] == 'L')
	{
		$zone_owner = $zoneinfo['owner'];
		
		$result = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_ships}
			WHERE ship_id = {int:zone_owner}',
			array(
				'db_ships' => $dbtables['ships'],
				'zone_owner' => $zone_owner
			)
		);
		$zoneowner_info = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);

		if($zone_owner <> $playerinfo['ship_id'])
		{
			if($zoneowner_info['team'] != $playerinfo['team'] || $playerinfo['team'] == 0)
			{
				echo $txt['bnt_mines_nopermit'], '<br><br>';
				TextMainMenu();
				return;
			}
		}
	}

	if (!isset($nummines) || !isset($numfighters) || !isset($mode))
	{
		$availmines = NUMBER($playerinfo['torps']);
		$availfighters = NUMBER($playerinfo['ship_fighters']);
		
		echo '
		<form action="', $scripturl, '?action=blacknova;sa=mines" method="post">';
		
		$txt['bnt_mines_info1'] = str_replace("[sector]", $playerinfo['sector'], $txt['bnt_mines_info1']);
		$txt['bnt_mines_info1'] = str_replace("[mines]", NUMBER($total_sector_mines), $txt['bnt_mines_info1']);
		$txt['bnt_mines_info1'] = str_replace("[fighters]", NUMBER($total_sector_fighters), $txt['bnt_mines_info1']);
		echo $txt['bnt_mines_info1'], '<br><br>';
		
		$txt['bnt_mines_info2'] = str_replace("[mines]", $availmines, $txt['bnt_mines_info2']);
		$txt['bnt_mines_info2'] = str_replace("[fighters]", $availfighters, $txt['bnt_mines_info2']);
		
		echo '
		', $txt['bnt_mines_info2'], '<br>
		<br>', $txt['bnt_mines_deploy'], ' <input type="text" name="nummines" size="10" maxlength="10" value="', $playerinfo['torps'], '"> ', $txt['bnt_mines'], '.<br>
		<br>', $txt['bnt_mines_deploy'], ' <input type="text" name="numfighters" size="10" maxlength="10" value="', $playerinfo['ship_fighters'], '"> ', $txt['bnt_fighters'], '.<br>
		<br>', $txt['bnt_mines_fmode'], '
		<br><input type="radio" style="check" name="mode" ', $set_attack, ' value="attack">', $txt['bnt_mines_att'], '</input>
		<br><input type="radio" style="check" name="mode" ', $set_toll, ' value="toll">', $txt['bnt_mines_toll'], '</input><br>
		<br><input type="submit" value="', $txt['bnt_submit'], '"><input type="reset" value="', $txt['bnt_reset'], '"><br><br>
		<br><input type="hidden" name="op" value=', $op, '">
		<br></form>';
	}
	else
	{
		$nummines = stripnum($nummines);
		$numfighters = stripnum($numfighters);
		
		if (empty($nummines))
			$nummines = 0;
		if (empty($numfighters))
			$numfighters = 0;
		if ($nummines < 0)
			$nummines = 0;
		if ($numfighters < 0)
			$numfighters = 0;
		
		if ($nummines > $playerinfo['torps'])
		{
			echo $txt['bnt_mines_notorps'], '<br>';
			$nummines = 0;
		}
		else
		{
			$txt['bnt_mines_dmines'] = str_replace("[mines]", $nummines, $txt['bnt_mines_dmines']);
			echo $txt['bnt_mines_dmines'], '<br>';
		}
		
		if ($numfighters > $playerinfo['ship_fighters'])
		{
			echo $txt['bnt_mines_nofighters'], '.<br>';
			$numfighters = 0;
		}
		else
		{
			$txt['bnt_mines_dfighter'] = str_replace("[fighters]", $numfighters, $txt['bnt_mines_dfighter']);
			$txt['bnt_mines_dfighter'] = str_replace("[mode]", $mode, $txt['bnt_mines_dfighter']);
			echo $txt['bnt_mines_dfighter'], '<br>';
		}

		if ($numfighters > 0)
		{
			if ($fighter_id != 0)
			{
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_sector_defence}
					SET quantity = quantity + {int:numfighters},
						fm_setting = {string:mode}
					WHERE defence_id = {int:fighter_id}',
					array(
						'db_sector_defence' => $dbtables['sector_defence'],
						'numfighters' => $numfighters,
						'mode' => $mode,
						'fighter_id' => $fighter_id
					)
				);
			}
			else
			{
				$smcFunc['db_insert']('insert',
					$dbtables['sector_defence'],
					array(
						'ship_id' => 'int',
						'sector_id' => 'int',
						'defence_type' => 'string',
						'quantity' => 'int',
						'fm_setting' => 'string'
					),
					array(
						$playerinfo['ship_id'],
						$playerinfo['sector'],
						"F",
						$numfighters,
						$mode
					),
					''
				);
			}
		}

		if ($nummines > 0)
		{
			if ($mine_id != 0)
			{
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_sector_defence}
					SET quantity = quantity + {int:nummines},
						fm_setting = {string:mode}
					WHERE defence_id = {int:mine_id}',
					array(
						'db_sector_defence' => $dbtables['sector_defence'],
						'nummines' => $nummines,
						'mode' => $mode,
						'mine_id' => $mine_id
					)
				);
			}
			else
			{
				$smcFunc['db_insert']('insert',
					$dbtables['sector_defence'],
					array(
						'ship_id' => 'int',
						'sector_id' => 'int',
						'defence_type' => 'string',
						'quantity' => 'int',
						'fm_setting' => 'string'
					),
					array(
						$playerinfo['ship_id'],
						$playerinfo['sector'],
						"M",
						$nummines,
						$mode
					),
					''
				);
			}
		}
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET last_login = NOW(),
				turns = turns - 1,
				turns_used = turns_used + 1,
				ship_fighters = ship_fighters - {int:numfighters},
				torps = torps - {int:nummines}
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'numfighters' => $numfighters,
				'nummines' => $nummines,
				'ship_id' => $playerinfo['ship_id']
			)
		);
	}
}

//-------------------------------------------------------------------------------------------------

TextMainMenu();

?>
